package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        //设置用户id，指定当前是哪个用户在操作购物车
        User user = (User) session.getAttribute("user");
        Long id = user.getId();
        shoppingCart.setUserId(id);
        //判断添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, id);
        if (dishId != null) {//如果dishid不是null则添加的是菜品，因为一次只能添加一种，不是菜品就是套餐。
            qw.eq(ShoppingCart::getDishId, dishId);

        } else {//否则添加的就是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查出来数据库中唯一一个菜品/套餐数据
        ShoppingCart one = shoppingCartService.getOne(qw);

        //判断查出来的套餐/菜品，数据库中有没有，由此来判断是不是第一次添加
        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            //不是第一次添加就将这个更新进去
            shoppingCartService.updateById(one);
        } else {  //查询数据库中没有，是第一次添加，则设置为1，并添加进数据库
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> select() {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.get());
        qw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long dishId = shoppingCart.getDishId();
        User user = (User) session.getAttribute("user");
        Long userId = user.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {  //如果查出来的是菜品数据
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(qw);

        Integer number = one.getNumber();

        one.setNumber(number - 1);

        if (number > 0) {
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.removeById(one);
        }



        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> delete(HttpSession session){
        User user = (User) session.getAttribute("user");
        Long id = user.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,id);
        shoppingCartService.remove(qw);
        return R.success("删除成功");
    }

}
